package com.xavier.convertor;

import cn.hutool.core.util.ObjectUtil;
import com.xavier.utils.BeanUtil;
import com.xavier.model.Role;
import com.xavier.dto.RoleDTO;
import com.xavier.vo.RolePageVO;
import com.xavier.vo.RoleDetailVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 角色对象相互转换
 *
 * @author Xavier
 * @version 1.0
 */
public abstract class RoleConvertor {

    public static RolePageVO toRolePageVO(Role role) {
        if (Objects.isNull(role)) {
            return null;
        }
        RolePageVO vo = BeanUtil.beanCopy(role, RolePageVO.class);
        return vo;
    }

    public static RoleDetailVO toRoleDetailVO(Role role) {
        if (Objects.isNull(role)) {
            return null;
        }
        RoleDetailVO vo = BeanUtil.beanCopy(role, RoleDetailVO.class);
        return vo;
    }

    public static Role toRole(RoleDTO dto) {
        if (Objects.isNull(dto)) {
            return null;
        }
        Role role = BeanUtil.beanCopy(dto, Role.class);
        return role;
    }

    public static List<RolePageVO> toRolePageVOList(List<Role> roleList) {
        if (ObjectUtil.isEmpty(roleList)) {
            return new ArrayList<>();
        }
        List<RolePageVO> roleInfoList = new ArrayList<>(roleList.size());
        for (Role role : roleList) {
            roleInfoList.add(toRolePageVO(role));
        }
        return roleInfoList;
    }

    public static List<Role> toRoleList(List<RoleDTO> dtoList) {
        if (ObjectUtil.isEmpty(dtoList)) {
            return new ArrayList<>();
        }
        List<Role> roleList = new ArrayList<>(dtoList.size());
        for (RoleDTO dto : dtoList) {
            roleList.add(toRole(dto));
        }
        return roleList;
    }

}
